mainfolder="mainfolder"; 
setwd(mainfolder)

library(foreach)    
library(doParallel) 
library(parallel)   
library(fanplot)
library(grid)
library(dplyr)
library(ggplot2)
library(foreign)
library(reshape2)
library(mapproj)
library(maptools)
library(rgeos)
library(plyr)
library(rgdal)
library(RColorBrewer)
library(spdep)
library(sf)
library(Rcpp)
library(ggfx)

numCores=3;#how many cores to run the simulation simultaneously.
tryno=3;#how many times the simulation will run.

setting="Preferred"


if(setting=="Preferred"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0.8;
  Ydiscount_hot=0.8;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startloess";
  UniformY=0;
  conflictmech="neighborY";
  river=1;
  Theta_steppe=-1;
  Psi_steppe=3;
  y_steppe=1116.475;
}

if(setting=="Adj. for hot and cold climate"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0.8;
  Ydiscount_hot=0.8;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}

if(setting=="Nomadic Pastoralism"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=1116.475;
}

if(setting=="Silk Road+Steppe Expansionism"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=-1;
  Psi_steppe=3;
  y_steppe=NA;
}


if(setting=="Loess"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startloess";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}

if(setting=="Rivers"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=1;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="Alt. Conflict Mech: Random"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="random";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="Alt. Conflict Mech: Expected Gain"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="expgain";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="Alt. r:KK10"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startKK10";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="Alt. Y CSI"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YCSI";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="No Obstacles+Uniform Resource"){
  Theta_cold=0;
  Theta_hot=0;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=0;
  sigma=1;
  alpha_sea=1;
  yielddta="";
  BC1000Ratio="";
  UniformY=1;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="Uniform Resource"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="";
  BC1000Ratio="";
  UniformY=1;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="No Obstacles"){
  Theta_cold=0;
  Theta_hot=0;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=0;
  sigma=1;
  alpha_sea=1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


if(setting=="No Climatic Obstacles"){
  Theta_cold=0;
  Theta_hot=0;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}

if(setting=="Baseline"){
  Theta_cold=2;
  Theta_hot=2;
  Ydiscount_cold=0;
  Ydiscount_hot=0;
  Theta_rugged_90th=2;
  sigma=0.333;
  alpha_sea=0.1;
  yielddta="YGAEZ";
  BC1000Ratio="startDSMW";
  UniformY=0;
  conflictmech="neighborY";
  river=0;
  Theta_steppe=0;
  Psi_steppe=1;
  y_steppe=NA;
}


Africa_Start=0;#Start Time for SubSaharan Africa; -->150
America_Start=0;#Start Time for the Americas -->450
Australia_Start=0;#Start Time for Australia
Japan_Start=0;#Start Time for Japan

Maize1_Start=0;#
Maize2_Start=0;# -->300
Maize3_Start=0;#-->450

y_steppeeast=NA;#3349.425;





######################Model Specification###################;
beta=0.00001;#succession coefficient.
shockprob_regime=0;
shockprob_general=0;

maxseadist=6;#maximum range of sea conflict (measured by the number of hexagons).
start_with_regime=0;#1 for start from defined regime boundary;0 for start from pixels


ymin=0.001;
unrest_min=0.000001;


###########################################################;

Tmax=500#The number of periods that the code will run.
drawmap=0;#"1" to plot maps; "0" otherwise.
Tplot=100;#Interval between consecutive maps.
drawEurasia=1;#1 for drawing Eurasia map; 0 for drawing the world map.

##########################################################;

plotcode=paste0(mainfolder,"Plotcode.R")
mapfile=paste0(mainfolder,"map/World5_Nodta")
mapfile_data=paste0(mainfolder,"map/World5")
mapfile_eurasia=paste0(mainfolder,"map/Euroasia_Dis")
mapfile_china=paste0(mainfolder,"map/China")
mapfile_europe=paste0(mainfolder,"map/Europe")

folder=paste0(mainfolder,"Output/")
herfEU=matrix(,nrow=tryno,ncol=Tmax);herfCN=matrix(,nrow=tryno,ncol=Tmax);herfMed=matrix(,nrow=tryno,ncol=Tmax);herfMideast=matrix(,nrow=tryno,ncol=Tmax);herfIndia=matrix(,nrow=tryno,ncol=Tmax);herfSEAsia=matrix(,nrow=tryno,ncol=Tmax);herfEuropaX=matrix(,nrow=tryno,ncol=Tmax);
EUmax.pixel=c();CNmax.pixel=c();
EUmax=c();CNmax=c();

Empireno_AfricaET=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AfricaWT=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AfricaME=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AfricaNH=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AfricaSH=matrix(,nrow=tryno,ncol=Tmax);

Empireno_AmericaNH=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AmericaSH=matrix(,nrow=tryno,ncol=Tmax);
Empireno_AmericaCL=matrix(,nrow=tryno,ncol=Tmax);


IntersectCN=matrix(,nrow=tryno,ncol=Tmax);
IntersectEU=matrix(,nrow=tryno,ncol=Tmax);
UnionCN=matrix(,nrow=tryno,ncol=Tmax);
UnionEU=matrix(,nrow=tryno,ncol=Tmax);


regimeno.EU=matrix(,nrow=tryno,ncol=Tmax);
regimesize.EU=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.CN=matrix(,nrow=tryno,ncol=Tmax);
regimesize.CN=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.Med=matrix(,nrow=tryno,ncol=Tmax);
regimesize.Med=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.Mideast=matrix(,nrow=tryno,ncol=Tmax);
regimesize.Mideast=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.India=matrix(,nrow=tryno,ncol=Tmax);
regimesize.India=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.SEAsia=matrix(,nrow=tryno,ncol=Tmax);
regimesize.SEAsia=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.EuropaX=matrix(,nrow=tryno,ncol=Tmax);
regimesize.EuropaX=matrix(,nrow=tryno*10,ncol=Tmax);

regimeno.AfricaET=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AfricaET=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AfricaWT=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AfricaWT=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AfricaME=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AfricaME=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AfricaNH=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AfricaNH=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AfricaSH=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AfricaSH=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AmericaNH=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AmericaNH=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AmericaSH=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AmericaSH=matrix(,nrow=tryno*10,ncol=Tmax);
regimeno.AmericaCL=matrix(,nrow=tryno,ncol=Tmax);
regimesize.AmericaCL=matrix(,nrow=tryno*10,ncol=Tmax);






map<-readOGR(dsn=paste0(mapfile,".shp"));EAdta=read.dbf(paste0(mapfile_data,".dbf"))
map_eurasia<-readOGR(dsn=paste0(mapfile_eurasia,".shp"))
map_china<-readOGR(dsn=paste0(mapfile_china,".shp"))
map_europe<-readOGR(dsn=paste0(mapfile_europe,".shp"))

pixel.borderM_land=poly2nb(map, queen=FALSE)
pixel.borderM_land=sapply(pixel.borderM_land,function(x) if(length(x)==1 && x==0){return(integer(0))}else{return(x)})


coastnear_table=read.csv(paste0(mainfolder,"map/DistMatrix_World.csv"))
coastnear_table=coastnear_table[coastnear_table$Dist<=maxseadist,]
coastnear_list=split(coastnear_table$NbList,coastnear_table$coastid)
coastfid=as.numeric(names(coastnear_list))


pixel.borderM=pixel.borderM_land
pixel.borderM[coastfid]=mapply(function(x,y) unique(c(x,y)),pixel.borderM[coastfid],coastnear_list,SIMPLIFY = FALSE)
pixel.single=which(sapply(pixel.borderM,function(x) if(length(x)==0){return(TRUE)}else{return(FALSE)})==TRUE)


print("simulation begins! find the simulation results in the output folder")
cl <- makeCluster(numCores) 

varlist=c("y_steppeeast","y_steppe","Theta_steppe","Psi_steppe","alpha_sea","Theta_rugged_90th","Theta_cold","Theta_hot","coastnear_list","coastnear_table","EAdta",
                         "pixel.borderM","pixel.borderM_land","Ydiscount_cold","Ydiscount_hot",
                         "CNmax.pixel","coastfid","sigma",
                         "drawmap","EUmax.pixel","Africa_Start","America_Start","Australia_Start","Japan_Start",
                         "folder","mainfolder","mapfile","mapfile_data",
                         "conflictmech","beta","ymin","unrest_min","shockprob_regime","shockprob_general",
                         "Tmax","pixel.single","plotcode","yielddta","river",
                         "UniformY","Tplot","tryno","Maize1_Start","Maize2_Start","Maize3_Start",
                         "BC1000Ratio","map_eurasia","map_china","map_europe","drawEurasia","start_with_regime")
if(drawmap==1){varlist=c(varlist,"map")}


registerDoParallel(cl) 
clusterEvalQ(cl, .libPaths("/Library/Frameworks/R.framework/Versions/3.6/Resources/library")) 
clusterExport(cl=cl,varlist=varlist, envir=environment())
system.time(
outdta<-foreach(roundI=seq(1,tryno),
        .packages = c("doParallel", "fanplot", "grid","dplyr","ggplot2","foreign","reshape2","mapproj","maptools","rgeos","plyr","rgdal","RColorBrewer","spdep","sf","Rcpp","ggfx")
        ) %dopar%  {  

 source("Maincode.R",local=TRUE)
        
       
}
) 
stopCluster(cl) 


##############################


for(i in seq(1,tryno)){
  herfEU[i,1:Tmax]=outdta[[i]]$value$Hihis.EU[1:Tmax]
  herfCN[i,1:Tmax]=outdta[[i]]$value$Hihis.CN[1:Tmax]
  herfMed[i,1:Tmax]=outdta[[i]]$value$Hihis.Med[1:Tmax]
  herfMideast[i,1:Tmax]=outdta[[i]]$value$Hihis.Mideast[1:Tmax]
  herfIndia[i,1:Tmax]=outdta[[i]]$value$Hihis.India[1:Tmax]
  herfSEAsia[i,1:Tmax]=outdta[[i]]$value$Hihis.SEAsia[1:Tmax]
  herfEuropaX[i,1:Tmax]=outdta[[i]]$value$Hihis.EuropaX[1:Tmax]
  
  
  
  regimeno.EU[i,1:Tmax]=outdta[[i]]$value$regimeno.EU[1:Tmax]
  regimeno.CN[i,1:Tmax]=outdta[[i]]$value$regimeno.CN[1:Tmax]
  regimeno.Med[i,1:Tmax]=outdta[[i]]$value$regimeno.Med[1:Tmax]
  regimeno.Mideast[i,1:Tmax]=outdta[[i]]$value$regimeno.Mideast[1:Tmax]
  regimeno.India[i,1:Tmax]=outdta[[i]]$value$regimeno.India[1:Tmax]
  regimeno.SEAsia[i,1:Tmax]=outdta[[i]]$value$regimeno.SEAsia[1:Tmax]
  regimeno.EuropaX[i,1:Tmax]=outdta[[i]]$value$regimeno.EuropaX[1:Tmax]
  
  
  regimesize.EU[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.EU[1:10,1:Tmax]
  regimesize.CN[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.CN[1:10,1:Tmax]
  regimesize.Med[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.Med[1:10,1:Tmax]
  regimesize.Mideast[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.Mideast[1:10,1:Tmax]
  regimesize.India[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.India[1:10,1:Tmax]
  regimesize.SEAsia[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.SEAsia[1:10,1:Tmax]
  regimesize.EuropaX[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.EuropaX[1:10,1:Tmax]
  
  regimeno.AfricaET[i,1:Tmax]=outdta[[i]]$value$regimeno.AfricaET[1:Tmax]
  regimesize.AfricaET[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AfricaET[1:10,1:Tmax]
  regimeno.AfricaWT[i,1:Tmax]=outdta[[i]]$value$regimeno.AfricaWT[1:Tmax]
  regimesize.AfricaWT[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AfricaWT[1:10,1:Tmax]
  regimeno.AfricaME[i,1:Tmax]=outdta[[i]]$value$regimeno.AfricaME[1:Tmax]
  regimesize.AfricaME[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AfricaME[1:10,1:Tmax]
  regimeno.AfricaNH[i,1:Tmax]=outdta[[i]]$value$regimeno.AfricaNH[1:Tmax]
  regimesize.AfricaNH[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AfricaNH[1:10,1:Tmax]
  regimeno.AfricaSH[i,1:Tmax]=outdta[[i]]$value$regimeno.AfricaSH[1:Tmax]
  regimesize.AfricaSH[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AfricaSH[1:10,1:Tmax]
  regimeno.AmericaNH[i,1:Tmax]=outdta[[i]]$value$regimeno.AmericaNH[1:Tmax]
  regimesize.AmericaNH[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AmericaNH[1:10,1:Tmax]
  regimeno.AmericaSH[i,1:Tmax]=outdta[[i]]$value$regimeno.AmericaSH[1:Tmax]
  regimesize.AmericaSH[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AmericaSH[1:10,1:Tmax]
  regimeno.AmericaCL[i,1:Tmax]=outdta[[i]]$value$regimeno.AmericaCL[1:Tmax]
  regimesize.AmericaCL[((i-1)*10+1):(i*10),1:Tmax]=outdta[[i]]$value$regimesize.AmericaCL[1:10,1:Tmax]
  
  
  
  
  EUmax.pixel[i]=outdta[[i]]$value$EUmax.orgpixel
  CNmax.pixel[i]=outdta[[i]]$value$CNmax.orgpixel
  EUmax[i]=outdta[[i]]$value$EUmax
  CNmax[i]=outdta[[i]]$value$CNmax
  
  IntersectCN[i,1:Tmax]=outdta[[i]]$value$IntersectCN[1:Tmax]
  IntersectEU[i,1:Tmax]=outdta[[i]]$value$IntersectEU[1:Tmax]
  
  UnionCN[i,1:Tmax]=outdta[[i]]$value$UnionCN[1:Tmax]
  UnionEU[i,1:Tmax]=outdta[[i]]$value$UnionEU[1:Tmax]

}


setwd(folder)
source(plotcode)  

write.csv(data.frame(round=1:tryno,HerfCN=herfCN[,Tmax],HerfEU=herfEU[,Tmax],CNmax=CNmax,EUmax=EUmax),"Herflast.csv")

rownames(herfEU)=paste0("sim",seq(1,tryno))
rownames(herfCN)=paste0("sim",seq(1,tryno))
rownames(herfMed)=paste0("sim",seq(1,tryno))
write.csv(t(herfEU),"herfEU.csv")
write.csv(t(herfCN),"herfCN.csv")
write.csv(t(herfMed),"herfMed.csv")

write.csv(t(IntersectCN),"IntstCN.csv")
write.csv(t(IntersectEU),"IntstEU.csv")
write.csv(t(UnionCN),"UnionCN.csv")
write.csv(t(UnionEU),"UnionEU.csv")
write.csv(t(IntersectCN)/t(UnionCN),"MatchIdxCN.csv")
write.csv(t(IntersectEU)/t(UnionEU),"MatchIdxEU.csv")


write.csv(t(regimeno.EU),"regimeno.EU.csv")
write.csv(t(regimeno.CN),"regimeno.CN.csv")
write.csv(t(regimeno.India),"regimeno.India.csv")
write.csv(t(regimeno.Mideast),"regimeno.Mideast.csv")
write.csv(t(regimeno.SEAsia),"regimeno.SoutheastAsia.csv")
write.csv(t(regimeno.EuropaX),"regimeno.EuropaX.csv")

rowname=do.call(paste0,expand.grid(paste0("nation",seq(1,10)),paste0("sim",seq(1,tryno))))
rownames(regimesize.EU)=rowname
rownames(regimesize.CN)=rowname
rownames(regimesize.India)=rowname
rownames(regimesize.Mideast)=rowname
rownames(regimesize.SEAsia)=rowname
rownames(regimesize.EuropaX)=rowname

write.csv(t(regimesize.EU),"regimesize.EU.csv")
write.csv(t(regimesize.CN),"regimesize.CN.csv")
write.csv(t(regimesize.India),"regimesize.India.csv")
write.csv(t(regimesize.Mideast),"regimesize.Mideast.csv")
write.csv(t(regimesize.SEAsia),"regimesize.SoutheastAsia.csv")
write.csv(t(regimesize.EuropaX),"regimesize.EuropaX.csv")



write.csv(t(regimeno.AfricaET),"regimeno.AfricaET.csv")
rownames(regimesize.AfricaET)=rowname
write.csv(t(regimesize.AfricaET),"regimesize.AfricaET.csv")

write.csv(t(regimeno.AfricaWT),"regimeno.AfricaWT.csv")
rownames(regimesize.AfricaWT)=rowname
write.csv(t(regimesize.AfricaWT),"regimesize.AfricaWT.csv")

write.csv(t(regimeno.AfricaME),"regimeno.AfricaME.csv")
rownames(regimesize.AfricaME)=rowname
write.csv(t(regimesize.AfricaME),"regimesize.AfricaME.csv")

write.csv(t(regimeno.AfricaNH),"regimeno.AfricaNH.csv")
rownames(regimesize.AfricaNH)=rowname
write.csv(t(regimesize.AfricaNH),"regimesize.AfricaNH.csv")

write.csv(t(regimeno.AfricaSH),"regimeno.AfricaSH.csv")
rownames(regimesize.AfricaSH)=rowname
write.csv(t(regimesize.AfricaSH),"regimesize.AfricaSH.csv")

write.csv(t(regimeno.AmericaNH),"regimeno.AmericaNH.csv")
rownames(regimesize.AmericaNH)=rowname
write.csv(t(regimesize.AmericaNH),"regimesize.AmericaNH.csv")

write.csv(t(regimeno.AmericaSH),"regimeno.AmericaSH.csv")
rownames(regimesize.AmericaSH)=rowname
write.csv(t(regimesize.AmericaSH),"regimesize.AmericaSH.csv")

write.csv(t(regimeno.AmericaCL),"regimeno.AmericaCL.csv")
rownames(regimesize.AmericaCL)=rowname
write.csv(t(regimesize.AmericaCL),"regimesize.AmericaCL.csv")

